package configs

import com.fasterxml.jackson.core.type.TypeReference
import tools.JsonUtil
import tools.config.Property

object CubeConfig {

    @Property(key = "filter.list", defaultValue = "")
    var FILTER_LIST: String = ""

    @Property(key = "chance.data", defaultValue = "{\"0\":{\"1\":10,\"2\":10,\"3\":5},\"5062000\":{\"1\":10,\"2\":5,\"3\":0},\"5062001\":{\"1\":10,\"2\":5,\"3\":0},\"5062002\":{\"1\":7.1,\"2\":3,\"3\":1.1},\"5062005\":{\"1\":10,\"2\":10,\"3\":5},\"5062006\":{\"1\":10,\"2\":10,\"3\":5},\"5062009\":{\"1\":10,\"2\":10,\"3\":5},\"5062010\":{\"1\":15,\"2\":10,\"3\":5},\"5062090\":{\"1\":11,\"2\":10,\"3\":5},\"5062100\":{\"1\":10,\"2\":10,\"3\":5},\"5062103\":{\"1\":10,\"2\":10,\"3\":5},\"5062500\":{\"1\":10,\"2\":10,\"3\":5},\"5062501\":{\"1\":10,\"2\":10,\"3\":5},\"5062503\":{\"1\":10,\"2\":10,\"3\":5},\"5062024\":{\"1\":10.10,\"2\":10,\"3\":5},\"5062502\":{\"1\":4.76,\"2\":2.55,\"3\":0.97}}")
    var CHANCE_DATA: String = ""

    private val filter_list = ArrayList<Int>()
    private val chance_data = HashMap<Int, HashMap<Int, Int>>()

    fun isFilter(id: Int): Boolean = filter_list.contains(id)

    fun updateFilterList() {
        filter_list.clear()
        if (FILTER_LIST.isNotEmpty()) {
            FILTER_LIST.replace(" ", "").split(",").forEach { filter_list.add(it.toInt()) }
        }
    }

    fun getChance(itemid: Int, rank: Int): Int {
        val newItemID = if (chance_data.containsKey(itemid)) itemid else 0
        val chancelist = chance_data[newItemID] ?: HashMap()
        return (chancelist[rank] ?: 0) * 10
    }

    fun updateChanceData() {
        chance_data.clear()
        if (CHANCE_DATA.isNotEmpty()) {
            chance_data.putAll(JsonUtil.getMapperInstance().readValue<HashMap<Int, HashMap<Int, Int>>>(CHANCE_DATA, object : TypeReference<HashMap<Int, HashMap<Int, Int>>>() {}))
        }
    }
}